package com.sdhs.paas.nativedbdemo.app;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.commons.util.IdUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.TypeReference;
import com.sdhs.paas.nativedbdemo.bean.UcAppBean;
import com.sdhs.paas.nativedbdemo.dto.UcAppDTO;
import com.sdhs.paas.nativedbdemo.dto.req.UcAppReqDTO;
import com.sdhs.paas.nativedbdemo.dto.res.UcAppResDTO;
import com.sdhs.paas.nativedbdemo.service.UcAppService;
import com.sdhs.paas.sdk.base.common.BasePage;
import com.sdhs.paas.sdk.base.dto.PageInfoDTO;
import com.sdhs.paas.sdk.base.page.Page;
import com.sdhs.paas.sdk.base.utils.EntityDtoConvertUtil;
import com.sdhs.paas.sdk.core.PaasBaseRequest;
import com.sdhs.paas.sdk.core.PaasBaseResponse;
import com.sdhs.paas.sdk.core.RefreshableRestController;

@RefreshableRestController
@RequestMapping(value="/app")
public class UcAppController {
	
	@Autowired
	private UcAppService ucAppService;
	
	@PostMapping("/getPageAppList")
	public PaasBaseResponse<UcAppResDTO> getUcAppPageList(@RequestBody PaasBaseRequest<UcAppReqDTO> ucAppReqDTO){
		//将dto转为bean
		UcAppBean ucAppBean = EntityDtoConvertUtil.convertToEntity(ucAppReqDTO.getBody().getUcAppDTO(), UcAppBean.class);
		//获取前端传递的分页参数
		PageInfoDTO pageInfoDTO =  ucAppReqDTO.getBody().getPageInfo();
		BasePage pageRequest = new BasePage();
		pageRequest.setPage(pageInfoDTO.getPage());
		pageRequest.setCount(pageInfoDTO.getPageSize());
		Page<UcAppBean> page = ucAppService.getPageUcAppList(pageRequest, ucAppBean);
		
		UcAppResDTO ucAppResDTO = new UcAppResDTO();
		PageInfoDTO resPageInfoDTO = new PageInfoDTO();
		resPageInfoDTO.setPage(page.getCurPage());
        resPageInfoDTO.setPageSize(page.getPageSize());
        resPageInfoDTO.setPageCount(page.getPageCount());
        resPageInfoDTO.setRowCount(page.getTotalCount());
        ucAppResDTO.setPageInfo(resPageInfoDTO);
        List<UcAppBean> ucAppBeanList = page.getResult();
        List<UcAppDTO> dtoList = new ArrayList<UcAppDTO>();
		dtoList = EntityDtoConvertUtil.convertToDTOList(ucAppBeanList, new TypeReference<List<UcAppDTO>>() {});
		ucAppResDTO.setUcAppDTOList(dtoList);
		return new PaasBaseResponse<>(ucAppResDTO);
	}
	
	@PostMapping("/saveUcApp")
	public PaasBaseResponse<UcAppResDTO> saveUcApp(@RequestBody PaasBaseRequest<UcAppReqDTO> ucAppReqDTO){
		UcAppDTO appDTO = ucAppReqDTO.getBody().getUcAppDTO();
		UcAppBean ucAppBean = EntityDtoConvertUtil.convertToEntity(appDTO, UcAppBean.class);
		String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
		ucAppBean.setAppId(uuid);
		ucAppBean.setCtime(System.currentTimeMillis());
		ucAppBean.setFlag(0);
		ucAppBean.setUtime(System.currentTimeMillis());
		Boolean flag = ucAppService.saveUcApp(ucAppBean);
		appDTO.setAppId(uuid);
		UcAppResDTO ucAppResDTO = new UcAppResDTO();
		ucAppResDTO.setUcAppDTO(appDTO);
		return new PaasBaseResponse<>(ucAppResDTO);
	}
	
	@PostMapping("/updateUcApp")
	public PaasBaseResponse<UcAppResDTO> updateUcApp(@RequestBody PaasBaseRequest<UcAppReqDTO> ucAppReqDTO){
		UcAppDTO appDTO = ucAppReqDTO.getBody().getUcAppDTO();
		UcAppBean ucAppBean = EntityDtoConvertUtil.convertToEntity(appDTO, UcAppBean.class);
		ucAppBean.setUtime(System.currentTimeMillis());
		Boolean flag = ucAppService.updateUcApp(ucAppBean);
		UcAppResDTO ucAppResDTO = new UcAppResDTO();
		ucAppResDTO.setUcAppDTO(appDTO);
		return new PaasBaseResponse<>(ucAppResDTO);
	}
	
	@PostMapping("/getUcAppDetail")
	public PaasBaseResponse<UcAppResDTO> getUcAppDetail(@RequestBody PaasBaseRequest<UcAppReqDTO> ucAppReqDTO){
		UcAppDTO appDTO = ucAppReqDTO.getBody().getUcAppDTO();
		UcAppBean ucAppBean = EntityDtoConvertUtil.convertToEntity(appDTO, UcAppBean.class);
		ucAppBean = ucAppService.getUcAppDetail(ucAppBean.getAppId());
		appDTO = EntityDtoConvertUtil.convertToDTO(ucAppBean, UcAppDTO.class);
		UcAppResDTO ucAppResDTO = new UcAppResDTO();
		ucAppResDTO.setUcAppDTO(appDTO);
		return new PaasBaseResponse<>(ucAppResDTO);
	}
}
